//
//  CommodityCommentsViewController.m
//  DuBai
//
//  Created by darktm on 14-3-18.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "CommodityCommentsViewController.h"

#import "MyGradeViewController.h"

#import "CommodityCommentsCell.h"

#import "TaskWithGetGoodsComments.h"

#import "UIImageView+WebCache.h"

#import "Comments.h"

@interface CommodityCommentsViewController ()<WYHttpTaskDelegate,MyGradeViewControllerDelegate>

@property (nonatomic,strong)NSMutableArray *commentsArray;

@end

@implementation CommodityCommentsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.commentBtn.layer.cornerRadius = 13.0f;
    [self loadComments];
}


#pragma mark - tableview delegate -

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.commentsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommodityCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommodityCommentsCell" forIndexPath:indexPath];

    Comments *o = self.commentsArray[indexPath.row];
    cell.desc.text = o.content;
    cell.name.text = o.nikename;
    cell.grade.text = o.grade;
    [cell.image setImageWithURL:[NSURL URLWithString:o.icon] placeholderImage:[Utils imageWithColor:[UIColor redColor] size:cell.image.image.size]];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    return;
    MyGradeViewController *g = (MyGradeViewController *)[Utils storyBoardWithId:@"MyGradeViewController"];
    g.delegate = self;
    g.order = self.order;
    [self.navigationController pushViewController:g animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    if([segue.identifier isEqualToString:@"MyGradeViewController"]){
        MyGradeViewController *g = segue.destinationViewController;
        g.delegate = self;
    }
    
}

#pragma mark - customer delegate -

-(void)commentSuccess{
    
    [self loadComments];
}


#pragma mark - request delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetGoodsComments class]]){
        
        self.commentsArray = rsult;
        
        [self.tableView reloadData];
    }
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    DLog(@"%@",obj.networkOp.url);
}

#pragma mark - customer method -

-(void)loadComments{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    TaskWithGetGoodsComments *g = [[TaskWithGetGoodsComments alloc] init];
    g.delegate = self;
    g.goodid = @"5309b0bc2e2f940cec0041a9";
    [g start];
}


@end
